package com.wsl.service.impl;

import com.wsl.Entity.Person;
import com.wsl.repository.PersonRepository;
import com.wsl.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Author:WangShuliang
 * @Description:
 * @Date:Created in 11:45 2017/11/29
 * @Modified by:
 */
@Service
public class DemoServiceImpl implements DemoService {
    @Autowired
    PersonRepository personRepository;
    @Transactional(rollbackFor = {IllegalArgumentException.class})//制定特定异常时数据回滚
    @Override
    public Person savePersonWithRollBack(Person person) {
        Person person1=personRepository.save(person);
        if (person.getName().equals("王亮亮")){
            throw new IllegalArgumentException("王亮亮已经存在，数据将会滚");
        }
        return person1;
    }
    @Transactional(noRollbackFor = {IllegalArgumentException.class})//制定地钉异常时  数据不回滚
    @Override
    public Person savePersonWithoutRollBack(Person person) {
        Person person1 = personRepository.save(person);
        if (person.getName().equals("王亮亮")){
            throw  new IllegalArgumentException("王亮亮虽然已经存在，数据不会回滚");
        }
        return person1;
    }
    @Transactional(rollbackFor = {SQLIntegrityConstraintViolationException.class})
    @Override
    public Person sqlRollback(Person person) {
        Person person1 = personRepository.save(person);
        return person1;
    }


}